package com.lucasx.util;

import com.lucasx.entity.Velocity;
import org.dom4j.DocumentException;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by LucasX on 2016/10/22.
 */
public class TransUtil {

    /**
     * 将历史天气<b>风向</b>的值反解析成坐标角度
     *
     * @param windDirectionName
     * @result =
     */
    public static double convertDirection(String windDirectionName) {
        double result;
        switch (windDirectionName) {
            case "北风":
                result = Math.random() * 22.5;
                break;
            case "东北风":
                result = 45 + Math.random() * 22.5;
                break;
            case "东风":
                result = 90 + Math.random() * 22.5;
                break;
            case "东南风":
                result = 135 + Math.random() * 22.5;
                break;
            case "南风":
                result = 180 + Math.random() * 22.5;
                break;
            case "西南风":
                result = 225 + Math.random() * 22.5;
                break;
            case "西风":
                result = 270 + Math.random() * 22.5;
                break;
            case "西北风":
                result = 315 + Math.random() * 22.5;
                break;
            default:
                result = 90 + Math.random() * 22.5;
        }

        return result;
    }

    /**
     * 根据风力大小反解析成风速
     * 返回short数组,第一个元素为最小值,第二个元素为最大值
     *
     * @param windForceName
     * @return
     */
    public static short[] convertVelocity(String windForceName) throws IOException, DocumentException {
        List<Velocity> velocityList = FileUtil.getVelocityList(FileUtil.readXml(DataSimulator.WIND_XML_PATH, "velocitys"));

        String regex = "[^0-9]";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(windForceName);
        char[] chars = matcher.replaceAll("").toCharArray();
        Arrays.sort(chars);

        short[] result = new short[2];
        velocityList.forEach(velocity -> {
            if (velocity.getvLevel() == Short.parseShort(String.valueOf(chars[chars.length / 2]))) {
                result[0] = velocity.getMinVelocity();
                result[1] = velocity.getMaxVelocity();
            }
        });

        return result;
    }

    public static void main(String[] args) throws IOException, DocumentException {
        short[] shorts = TransUtil.convertVelocity("5级转3-6级");
        for (short s : shorts) {
            System.out.println(s);
        }
    }
}
